#version 460 core





#define M_PI 3.1415926535897932384626433832795

 
layout(points) in;
layout(line_strip, max_vertices=64) out;
//layout(lines, max_vertices=128) out;


#include "lib/project_matrix.glsl"
#include "lib/vertex_in.glsl"
#include "single_color.glsl"



//图元 顶点偏移和顶点数量 
layout(std430, column_major, binding = 5) readonly buffer U5_Matrix {
	uvec2 m_VertOffset_Num[];
};


in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	uint InstanceIndex;
}vertex_in[];


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};


void main(void){
	uint instance = vertex_in[0].InstanceIndex;
	mat3x2 mat = m_LP_SD[instance];
	
	vec2 size = mat[1] - mat[0];
	float PN = sign(size.x);
	
	vec4 TColor = f_buildColor(InstanceColor[instance]);
	uvec2 VertOffsetNum = m_VertOffset_Num[instance];


	uint num = VertOffsetNum.y - VertOffsetNum.x;
	for(uint i=0; i<num; ++i){
		vec4 v = vec4(m_Vertex[VertOffsetNum.x + i].coord, 0, 1);
		vec2 Weights = m_Vertex[VertOffsetNum.x + i].weight;
		vec2 normal = m_Vertex[VertOffsetNum.x + i].normal;

		//v.xy = (v.xy * mat[2].x)    +    ((mat[1]-mat[2].x) * Weights.xy);
		vec2 Invert = sign(mat[1]);
		v.xy = (v.xy * (mat[2].x) * Invert) + ((mat[1]-mat[2].x*Invert) * Weights.xy);
		
		
		v.xy += mat[0];
		v.xy *= m_Scale;
		v.xy += m_Loc;
		
		gl_Position = mat_ProjecView * v;
		gl_PointSize = mat[2].x;
		gColor = TColor;
		EmitVertex();
	}

	EndPrimitive();
}



 